* --------------------- *
* This file loads the mother and child level data to compute th ebalancing tests
* presented in Tables A1, A2 and A3.
* Author: Victor Ronda
* Date Created: 01 Mar 2020
* Last Update: 22 Jan 2023
* --------------------- *


clear all
set more off

use "mother_data.dta", replace
keep female pregorder* dob* age2017 cohort rear
keep if female==1

gen primip=0 if pregorder~=.
replace primip=1 if pregorder==1
egen numpreg_p1=rowmax(pregorder_ch1 pregorder_ch2 pregorder_ch3 pregorder_ch4 pregorder_ch5 pregorder_ch6 pregorder_ch7 pregorder_ch8 pregorder_ch9 pregorder_ch10 pregorder_ch11 pregorder_ch12 pregorder_ch13 pregorder_ch14 pregorder_ch15 pregorder_ch16)
gen numpreg=numpreg_p1
replace numpreg=0 if numpreg==. & female==1
replace numpreg=. if female==0
gen anyoffspring=1 if numpreg>0 & numpreg~=.
replace anyoffspring=0 if numpreg==0
gen age1preg2=(dob_ch1-dob)/365


gen anyoffspring_a8=anyoffspring if age2017>=8
gen numpreg_a8=numpreg if age2017>=8
gen numpreg_p1_a8=numpreg_p1 if age2017>=8 

local controls1 "cohort female primip"

gen MR_vs_SPR=1 if rear=="MR"
replace MR_vs_SPR=0 if rear=="PR"|rear=="SPR"

drop if cohort==. | female==. | primip==. | MR_vs_SPR==.
keep if anyoffspring_a8~=.

*****************
**** Table A1****
*****************

*Note: The following regressions compute the tests presented in Table A1

foreach var of varlist anyoffspring_a8 age1preg numpreg_a8 numpreg_p1_a8{
reg primip MR_vs_SPR if `var'~=.
tab MR_vs_SPR if `var'~=.
reg MR_vs_SPR if `var'~=.
test _cons=0.5
}

use "offspring_data.dta", replace


gen prim=0 if preg~=.
replace prim=1 if preg==1

replace stillborn=0 if stillborn==.

foreach var of varlist bw diedb1m {
replace `var'=. if stillborn==1
}

local outcomes1 "stillborn diedb1m bw"

local controls1 "female"
local controls2 "mom_cohort female mom_primip"

gen r_inte=0 if rear_inte==0|rear_inte==2
replace r_inte=1 if rear_inte==1|rear_inte==3
gen r_inte_MR=0 if rear_inte==0
replace r_inte_MR=1 if rear_inte==1
gen r_inte_PR=0 if rear_inte==2
replace r_inte_PR=1 if rear_inte==3

gen r_intra=0 if rear_inte==0|rear_inte==1
replace r_intra=1 if rear_inte==2|rear_inte==3
gen r_intra_MR=0 if rear_inte==0
replace r_intra_MR=1 if rear_inte==2
gen r_intra_PR=0 if rear_inte==1
replace r_intra_PR=1 if rear_inte==3

gen r1= mom_rear_MR_SPR
gen r2=r_inte_MR
gen r3=r_inte_PR
gen r4=r_intra
gen r5=r_intra_MR
gen r6=r_intra_PR

rename health_issue_*_10m3y hi_*_13
replace hi_m_13=1-hi_m_13
replace hi_m_13=. if obs_10m3y<=5

rename dominance* dom*
rename dom_relative_rank dom_pct

drop if mom_cohort==. | female==. | mom_primip==. | cohort==. | primip==. |mom_rear_MR_SPR==. 

*****************
**** Table A2****
*****************

*Note: The following regressions compute the tests presented in Table A2


foreach var of varlist stillborn diedb1m bw{
reg female mom_rear_MR if `var'~=.
reg primip mom_rear_MR if `var'~=.
reg mom_primip mom_rear_MR if `var'~=.
tab rear_MR if `var'~=.
reg mom_rear_MR if `var'~=.
test _cons=0.5
}

*****************
**** Table A3****
*****************

*Note: The following regressions compute the tests presented in Table A3


gen treat0=(rear_inte==0)
gen treat1=(rear_inte==1)
gen treat2=(rear_inte==2)
gen treat3=(rear_inte==3)

foreach var of varlist hi_m_13 dom_pct{
reg female treat1 treat2 treat3 if `var'~=.
test treat1 treat2 treat3
reg primip treat1 treat2 treat3 if `var'~=.
test treat1 treat2 treat3
reg mom_primip treat1 treat2 treat3 if `var'~=.
test treat1 treat2 treat3
tab rear_inte if `var'~=.
reg treat1 if (treat1==1 | treat0==1) & `var'~=.
test _cons=0.5
reg treat2 if (treat2==1 | treat0==1) & `var'~=.
test _cons=0.5
reg treat3 if (treat3==1 | treat0==1) & `var'~=.
test _cons=0.5
mlogit rear_inte if `var'~=. , b(0)
test [MR___Mother__S_PR]_cons=[_S_PR___Mother_MR]_cons=[_S_PR___Mother__S_PR]_cons
}
